/*******************************************************************************
Last update: September 19, 2025 
********************************************************************************/

/*******************************************************************************
Preliminaries
********************************************************************************/
cls
clear all
set more off

//Working directory
*local wd "~/Dropbox/Research/Distinction Effect/Replication Package"
local wd "D:/Dropbox/Distinction Effect/Replication Package"
cd "`wd'"

if substr(c(pwd), -19, 19) == "Replication Package" {
	
	//User-written programs used are stored here
	adopath ++ Does/ado/

	//Load Author-written programs
	local filelist: dir "Does/Analysis/Programs/" files "*.do"
	foreach i of local filelist {
	  qui do "Does/Analysis/Programs/`i'"
	}
}
else {
	break
	dis in red "WARNING: Working directory is not Replication Package"
}

/*******************************************************************************
Globals
********************************************************************************/

//Outcome
global Y ln_earningsMW
//Test Scores
global T reading english sb11score
//Covariates
global X female age stratum meduc_hs meduc_cllg2Y meduc_cllg4Y d_semester2 d_semester3 d_semester4
//Fixed Effects
global FE yarea_*
global FFE yfield_*

//Specification 1
global S1 $FE
//Specification 2
global S2 $T $FE
//Specification 3
global S3 $X $T $FE
	
//Kernel
global kernel "epanechnikov"
	
//SE cluster level
global SE year_area

//Common RDrobust common options
global RDoptions kernel($kernel) all vce(cluster $SE)
global RDoptionsRobust kernel($kernel) all vce(hc3)

//Running Variable
global running score_sd

/*******************************************************************************
Main Result
********************************************************************************/

use "Data/Finals/Estimation Sample.dta", clear
rdrobust $Y $running, $RDoptions bwselect(mserd) p(1) covs($S3)
global MSE_Bw = e(h_l)
global MSE_Bias = e(b_l)

/*******************************************************************************
Appendix Figures
********************************************************************************/

/***************************************************************************************************
Appendix Figure A.1: Probability of Taking the College Exit Exam as a Function of College Reputation
****************************************************************************************************/
use "Data\Finals\EnrollmentRecords_CollegeExam.dta", clear

//Year of Enrollment 
tostring prim_sem, gen(prim_year)
replace prim_year = substr(prim_year, 1, 4)
destring prim_year, replace

//Year of Enrollment 
tostring grado_per, gen(grad_year)
replace grad_year = substr(grad_year, 1, 4)
destring grad_year, replace

//Eligibility Criteria:
// Restrict to cohorts 2002 to 2007
keep if prim_year >= 2002 & prim_year <= 2007
// Keep students who graduated between 2006 and 2012 (or took the exam)
keep if grad_year >= 2006 & grad_year <= 2012
// Drop if document is "Tarjeta de Identidad"
drop if doc_tipo == "T"

replace spa_spro = 0 if exam_year > grad_year
replace exam_year = . if spa_spro == 0

//Drop fields without ECAES 2006 to 2009
drop if regex(program_field, "ARTE") | regex(program_field, "PLASTICAS")
drop if regex(program_field, "MILITAR")
drop if regex(program_field, "POLITICA")
drop if regex(program_field, "FILOSOFIA")
drop if regex(program_field, "BIOMEDICA")
drop if regex(program_field, "BIBLIOTECOLOGIA")
drop if program_field == "SALUD PUBLICA"
drop if program_field == "MUSICA"
drop if program_field == "DISENIO"
drop if program_field == "SIN CLASIFICAR"
drop if program_field == "OTRAS INGENIERIAS"

//College Information
preserve
use "Data\Originals\SNIES\Enrollment 2000-2019.dta", clear
duplicates drop cllg_code, force
keep cllg_code cllg_sector cllg_type cllg_muni_code
rename cllg_code ies
tempfile CllgInfo
save `CllgInfo', replace
restore 

merge m:1 ies using `CllgInfo'
keep if _merge == 3
drop _merge

//College Ranking
cap drop univ*
gen univ_1 = (ies==1101) //UNal
gen univ_2 = (ies == 1201) //Universidad de Antioquia
gen univ_3 = (ies == 1203) //Universidad del Valle
gen univ_4 = (ies == 1813) //Universidad de Los Andes
gen univ_5 = (ies == 1701) //Universidad Javeriana

gen univ_6 = (ies==1102) //UNal*
gen univ_7 = (ies == 1204) //Universidad Industrial de Santander
gen univ_8 = (ies == 1713) //Universidad del Norte
gen univ_9 = (ies == 1110) //Universidad del Cauca
gen univ_10 = (ies == 2711) //Universidad Tecnologica de Pereira*
gen univ_11 = (ies == 1112) //Universidad de Caldas
gen univ_12 = (ies == 1710) //Universidad Pontificia Bolivariana
gen univ_13 = (ies == 1106) //Universidad Tecnologica y Pedagogica*
gen univ_14 = (ies == 1205) //Universidad de Cartagena*
gen univ_15 = (ies == 1706) //Universidad Externado de Colombia
gen univ_16 = (ies == 1714) //Universidad del Rosario
gen univ_17 = (ies == 1301) //Universidad Distrital
gen univ_18 = (ies == 1704) //Universidad Santo Tomas*
gen univ_19 = (ies == 1712) //Universidad EAFIT
gen univ_20 = (ies == 1711) //Universidad de La Sabana

cap drop top5 top20 above20
gen top5 = 0
gen top20 = 0
foreach i of numlist 1(1)20 {
if `i' <= 5 {
replace top5 = 1 if univ_`i' == 1
}
if `i' >= 6 & `i' <= 20 {
replace top20 = 1 if univ_`i' == 1
}
}
gen above20 = (top5==0 & top20==0)

//Age at Enrollment
destring nac_ano, replace
gen age = prim_year - nac_ano
replace age = prim_year - year(spro_birthdate) if age == .
replace age = . if age < 15 | age > 45

//College Sector and Type
gen cllg_prv = cllg_sector == "PRIVADA"

//Socioeconomic Stratum 
gen d_SES = estrato == .
bys program_code prim_sem: egen aux = mean(estrato)
replace estrato = round(aux, 1) if estrato == . 
drop aux

gen high_SES = estrato >= 3 if estrato != .

//Pass Rate
sum materias_tomadas, d
replace materias_tomadas = . if materias_tomadas < r(p5)
replace materias_tomadas = . if materias_tomadas > 200

gen pass_rate = round(materias_aprobadas/materias_tomadas, 0.001)

gen d_pass_rate = pass_rate == .
bys program_code prim_sem: egen aux = mean(pass_rate)
replace pass_rate = round(aux, 0.001) if pass_rate == .
drop aux

//High School Exam Score
gen d_puntaje_icfes = puntaje_icfes == .
bys program_code prim_sem: egen aux = mean(puntaje_icfes)
replace puntaje_icfes = round(aux, 1) if puntaje_icfes == .
drop aux

//Gender
rename sexo female

//Sample Selection
drop if age == .
drop if female == .
drop if puntaje_icfes == .
drop if estrato == .

//Drop programs in which less than 5% take the exam
bys program_code : egen aux = mean(spa_spro)
drop if aux < 0.05 //4,630 obs
drop aux

save "Data\Finals\SProEligible.dta", replace

/***************************************************************************************************
Appendix Figure A.2: Relationship Between Students' Fields of Study
and Specific Exams
****************************************************************************************************/

* Run "4. AFigure Field of Study - Specific Exam"

/***************************************************************************************************
Appendix Figure A.3: Distinction Recipients by Field of Study and Exam Year
****************************************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear

keep if exam_year <= 2009
//Share of Awardees by Field
bys exam_field exam_year : egen share = mean(award)
	
//Count of Awardees
keep if award == 1
	
collapse (sum) award (mean) share, by(exam_field exam_year)
	
reshape wide award share, i(exam_field) j(exam_year)

foreach var of varlist award* {
	replace `var' = 0 if `var' == .
}	
	
foreach var of varlist share* {
	replace `var' = `var'*100
}
	
gen total09 = award2006 + award2007 + award2008 + award2009
gen total08 = award2006 + award2007 + award2008
gen total07 = award2006 + award2007	
gen total06 = award2006

save "Results/Online Appendix/Data/AwardeesbyExamYear.dta", replace
	
/***************************************************************************************************
Appendix Figure A.4: Google Trends Interest for National Distinction Award
****************************************************************************************************/

* Google trends weekly interest fro the national distiction award from January 2006 to December 2009.

/***************************************************************************************************
Appendix Figure A.5: Sample Report of Performance in the College Exit Exam
****************************************************************************************************/

* Example of a report card of performance of a student who took the economics field exam in 2009.

/***************************************************************************************************
Appendix Figure A.6: Placebo Tests and Differences in Earnings Between Contiguous Percentiles
****************************************************************************************************/
//Appendix Figure A.6 a
use "Data/Finals/Estimation Sample.dta", clear

keep if exam_year <= 2009 & $Y != . 
cap drop pscore
xtile pscore = $running, n(100)
tab pscore award

//RD on placebo cutoffs
matrix P = J(98, 3, .)
matrix colnames P = "BW" "RD" "SE"
foreach p of numlist 99(1)2 {
	cap drop score_placebo
	sum $running if pscore == `p' & award == 0
	gen score_placebo = $running - r(min) if award == 0
	rdrobust $Y score_placebo, $RDoptions bwselect(mserd) p(1) covs($S3)
	mat P[`p'-1, 1] = round(e(h_l), 0.001)
	mat P[`p'-1, 2] = tau_bc
	mat P[`p'-1, 3] = se_tau_rb
}

preserve
clear
svmat P, names(col)
gen percentile = _n
save "Results/Online Appendix/Data/PlaceboTest.dta", replace
restore

//Appendix Figure A.6 b

//Wage differential between percentiles
matrix P = J(98, 4, .)
matrix colnames P = "percentile" "coeff" "se" "df"			
foreach p of numlist 99(1)2 {
	cap drop award_placebo
	gen award_placebo = pscore >= `p'
	reghdfe $Y award_placebo $X $T if (pscore == `p' - 1 | pscore == `p'), absorb(year_area) cluster(year_area)
	mat P[`p'-1, 1] = `p'
	mat P[`p'-1, 2] = _b[award_placebo]
	mat P[`p'-1, 3] = _se[award_placebo]
	mat P[`p'-1, 4] = e(df_r)
}

preserve
clear
svmat P, names(col)
save "Results/Online Appendix/Data/EarningsDiffPlacebo.dta", replace	
restore

/*******************************************************************************
Appendix Figure C.1: Density Smoothness Around the Cutoff of the National Distinction Award
********************************************************************************/

//Appendix Figure C.1 a
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009

# delimit ;
tw  (kdensity score_sd if score_sd >= -8, lcolor("0 0 200") lw(thin))
	(histogram score_sd if score_sd >= -8, w(0.1) lw(thin) lcolor("0 0 200%50") fcolor("0 0 200%20"))
	,
	graphregion(color(white)) plotregion(lcolor(black)) xli(0, lcolor(black) lp(solid) lw(thin))
	ylab(0(0.05)0.4, format(%9.2f) angle(horizontal) labsize(medsmall) grid glwidth(thin) glcolor(gs10) glpattern(dot) nogextend) 
	xlab(-8(1)3, format(%9.1fc) labsize(medsmall) grid glwidth(thin) glcolor(gs10) glpattern(dot) nogextend)
	xti(" " "Distance to National Distinction Award cutoff ({&sigma})")
	yti("Density" " ") legend(off)	
; # delimit cr
gr export "Results/Online Appendix/NormalizedDensity.png", replace  wid(700) hei(500)

//Appendix Figure C.1 b
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & sample == 1

//By Field-Year
matrix M_Test = J(45, 17, .)
levelsof exam_id, local(Test)
local i = 1
foreach t of local Test {
	dis "`t'"
	mat M_Test[`i', 1] = `t'
	local j = 2
	foreach year of numlist 2006(1)2009 {
		dis `year'
		count if exam_id == `t' & exam_year == `year' & award == 1 & score_sd != . //adiciono el ultimo condicional
		local Nr = r(N)
		if `Nr' >= 10 {
			//Calonico et al.
			rddensity score_sd if exam_id ==  `t' & exam_year == `year', all
			mat M_Test[`i', `j'] = e(N_l)
			mat M_Test[`i', `j'+1] = e(N_r)
			mat M_Test[`i', `j'+2] = e(pv_q)
			}
		local j = `j' + 4
	}
	local i = `i' + 1
}

preserve
	gen n = 1
	collapse (sum) n, by(exam_id exam_field) 
	drop n
	tempfile FieldTests
	save `FieldTests', replace
	
	clear 
	svmat M_Test
	rename M_Test1 exam_id
	local t1 = 2
	local t2 = 3
	local t3 = 4
	local t4 = 5
	foreach year of numlist 2006(1)2009 {
		rename M_Test`t1' N_left`year'
		rename M_Test`t2' N_right`year'
		rename M_Test`t3' Calonico_p_val`year'
		replace Calonico_p_val`year' = round(Calonico_p_val`year', 0.001)
		local t1 = `t1' + 4
		local t2 = `t2' + 4
		local t3 = `t3' + 4
		local t4 = `t4' + 4
	}
	merge 1:1 exam_id using `FieldTests'
	drop _merge
	order exam_*
	save "Results/Online Appendix/Data/ManipulationTest.dta", replace	
restore

/***************************************************************************************************
Appendix Figure C.2: Field-Specific Exam Scores and RD First Stage
****************************************************************************************************/
//Appendix Figure C.2 a
use "Data/Finals/Estimation Sample.dta", clear

//Store all Tresholds
levelsof score_cutoff, local(cutoffs)
local tresholds = " "
foreach c of local cutoffs {
dis `c'
local tresholds = "`tresholds' xli(`c', lcolor(black) lp(dot))"
}

tempvar score
sum spro_puntaje
gen `score' = spro_puntaje if spro_puntaje >= r(mean)-7*r(sd)

//Plot Density with all Tresholds
# delimit ;
tw  (kdensity `score' , lcolor("0 0 200") lw(thin))
	(histogram `score', w(1) lcolor("0 0 200%50") lw(thin) fcolor("0 0 200%20"))
	,
	`tresholds' 
	ylab(0(0.01)0.05, format(%9.2f) angle(horizontal) labsize(medsmall) glwidth(none) nogextend)
	xlab(30(10)160, format(%9.0f) labsize(medsmall) glwidth(none) nogextend)	
	graphregion(color(white)) plotregion(lcolor(black)) 
	xti(" " "Field-Specific Score at College Exit Exam") 
	yti("Density" " ") legend(off)
	; # delimit cr
gr export "Results/Online Appendix/ScoresDensity.png", replace wid(2000) hei(1500)

//Appendix Figure C.2 b

use "Data/Finals/Estimation Sample.dta", clear

tempvar running prob_award
gen double `running' = .
gen double `prob_award' = .
local h = 0.1
local n = 1
qui foreach i of numlist -3(`h')1 {
	qui replace `running' = `i' in `n'
	if `i' != 0 {
	sum award if score_sd >= `i' - `h'/2 & score_sd < `i' + `h'/2    	    
	}
	else {
	sum award if score_sd >= 0 & score_sd < `h'/2	    
	}
	qui replace `prob_award' = r(mean) in `n'
	local n = `n' + 1
} 

# delimit ;
tw (scatter `prob_award' `running' , mfcolor(gs10) mlcolor(black) mlw(thin) msize(medsmall) )
	,
	graphregion(color(white)) plotregion(lcolor(black)) xli(0, lcolor(black) lp(solid) lw(thin))
	ylab(0(0.1)1, format(%9.1f) angle(horizontal) labsize(medsmall) grid glwidth(thin) glcolor(gs10) glpattern(dot) nogextend) 
	xlab(-3(0.5)1, format(%9.1fc) labsize(medsmall) grid glwidth(thin) glcolor(gs10) glpattern(dot) nogextend)
	xti(" " "Distance to National Distinction Award cutoff ({&sigma})") 
	yti("Pr( Winning National Award )" " ") legend(off)
;# delimit cr
gr export "Results/Online Appendix/RDProbDistinction_scores.png", replace  wid(700) hei(500)

/******************************************************************************
Appendix Figure C.3: Covariate Balance Around the Cutoff of the National Distinction Award
*******************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009

global X2 female age low_SES meduc_cllg feduc_cllg working cllg_prv top5 num_semesters sb11score reading english

foreach x of varlist $X2{
	ereplace `x' = std(`x')
}

egen balance_index = rowmean($X2)
gen balance_index_signed = (- low_SES - female - age - feduc_cllg + meduc_cllg + working + num_semesters + cllg_prv + top5 + sb11score + english + reading)/12
gen balance_index_absolute = (abs(low_SES) + abs(female) + abs(age) + abs(feduc_cllg) + abs(meduc_cllg) + abs(working) + abs(num_semesters) + abs(cllg_prv) + abs(top5) + abs(sb11score) + abs(english) + abs(reading))/12 

global X2 $X2 balance_index_absolute

label var balance_index "Balance Index"
label var balance_index_signed "Balance Index (Signed)"
label var balance_index_absolute "Covariate Index"

keep $X2 score_sd yarea_* year_area

global K = `: word count $X2'
cap drop varname
gen varname = ""
matrix Balance = J($K, 9, .)
matrix colnames Balance = "RD" "SE" "P_value" "Mean" "Observations" "BW_loc_poly" "BW_bias" "Effect_obs_left" "Effect_obs_right"

local i = 1
foreach x of varlist $X2 {

	//Summary Stat
	qui sum `x' if score_sd != .
	global mu = r(mean)

	//Residualize
	rdrobust `x' score_sd, p(1) kernel(epa) all bwselect(mserd) covs(yarea_*) vce(cluster year_area)
		
	global BW = e(h_l)

	replace varname = "`: variable label `x''" in `i'
		
	//Store Results
	mat Balance[`i', 1] = e(tau_bc)
	mat Balance[`i', 2] = e(se_tau_rb)
	local tc = abs(e(tau_bc)/e(se_tau_rb))
	mat Balance[`i', 3] = (1-normal(`tc'))*2
	mat Balance[`i', 4] = $mu
	mat Balance[`i', 5] = e(N)
	mat Balance[`i', 6] = round(e(h_l), 0.001)
	mat Balance[`i', 7] = round(e(b_l), 0.001)
	mat Balance[`i', 8] = e(N_h_l)
	mat Balance[`i', 9] = e(N_h_r)

	local i = `i' + 1
}

//Graph
preserve
	keep varname
	keep in 1/$K
	svmat Balance, names(col)
	save "Results/Online Appendix/Data/RDCovContinuity_std.dta", replace
restore


/***************************************************************************************************
Appendix Figures C.4 & C.5: Continuity in Pretreatment Covariates
****************************************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear

rdrobust $Y score_sd, all vce(cluster year_area) kernel(epa) bwselect(cerrd) p(1) covs(yarea_* $X $T)
global BW = e(h_l)
global PW = e(b_l)

global Covs female age low_SES high_SES meduc_cllg feduc_cllg working main_city cllg_prv top5 num_semesters sb11score reading english
keep $Covs score_sd yarea_* year_area cllg_reputation

//Plots
tempvar score
gen double `score' = score_sd
replace `score' = 1.19 if `score' >= 1.2 & `score' != .

foreach x of varlist meduc_cllg feduc_cllg female cllg_prv top5 working high_SES {
    local y_title = "`: variable label `x''"
	
	RDplot `x' `score', z0(-3) z1(1.2) h(0.1) wid(0.1) degree(1) kernel(gaus) bw($BW) pw($PW) ylab(0(0.1)0.9) xlab(-3(0.5)1) yformat(%9.1fc) ytitle("Pr( `y_title' )")
	gr export "Results/Online Appendix/RD`x'.png", replace wid(2000) hei(1500)
}

RDplot age `score', z0(-3) z1(1.2) h(0.1) wid(0.1) degree(1) kernel(gaus) bw($BW) pw($PW) ylab(22(0.5)27) xlab(-3(0.5)1) yformat(%9.1fc) ytitle("Age at Test Date")
gr export "Results/Online Appendix/RDage.png", replace wid(2000) hei(1500)

RDplot reading `score', z0(-3) z1(1.2) h(0.1) wid(0.1) degree(1) kernel(gaus) bw($BW) pw($PW) ylab(-0.25(0.25)2.5) xlab(-3(0.5)1) ytitle("College Exit Exam: Reading Score ({&sigma})")
gr export "Results/Online Appendix/RDreading.png", replace wid(2000) hei(1500)

RDplot english `score', z0(-3) z1(1.2) h(0.1) wid(0.1) degree(1) kernel(gaus) bw($BW) pw($PW) ylab(-0.25(0.25)2.5) xlab(-3(0.5)1) ytitle("College Exit Exam: English Score ({&sigma})")
gr export "Results/Online Appendix/RDenglish.png", replace wid(2000) hei(1500)

RDplot sb11score `score', z0(-3) z1(1.2) h(0.1) wid(0.1) degree(1) kernel(gaus) bw($BW) pw($PW) ylab(-0.25(0.25)2.5) xlab(-3(0.5)1) ytitle("High School Exit Exam: Overall Score ({&sigma})")
gr export "Results/Online Appendix/RDsb11score.png", replace wid(2000) hei(1500)

RDplot cllg_reputation `score', z0(-3) z1(1.2) h(0.1) wid(0.1) degree(1) kernel(gaus) bw($BW) pw($PW) ylab(65(5)95) yformat(%9.0f) xlab(-3(0.5)1) ytitle("College Reputation")
gr export "Results/Online Appendix/RDcllgreputation.png", replace wid(2000) hei(1500)

/*******************************************************************************
Appendix Figure C.6: Sample Selection Around the Cutoff of the National Distinction Award
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009

//Appendix Figure C.6 a 
cap drop ba_*
foreach year of numlist 2007(1)2016 {
	gen ba_`year' = grad_year <= `year' & exam_time <= grad_time & grad_time != .
}

mat B = J(2016-2007+1, 7, .)
matrix colnames B = "RD" "SE" "Mean_left" "Observations" "BW_loc_poly" "Effect_obs_left" "Effect_obs_right"

local i = 1
foreach year of numlist 2007(1)2016 {

	rdrobust ba_`year' score_sd , p(1) kernel(epa) all bwselect(mserd) vce(cluster year_area) covs(yarea_*)
	mat B[`i', 1] = e(tau_bc)
	mat B[`i', 2] = e(se_tau_rb)
	mat B[`i', 3] = e(tau_bc_l)
	mat B[`i', 4] = e(N)
	mat B[`i', 5] = round(e(h_l), 0.001)
	mat B[`i', 6] = e(N_h_l)
	mat B[`i', 7] = e(N_h_r)

	local i = `i' + 1
}

preserve
clear
svmat B, names(col)
save "Results/Online Appendix/Data/RDBADegree.dta", replace
restore

//Pooling
//Footnote 47
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009
cap drop ba_*
foreach year of numlist 2007(1)2016 {
	gen ba_`year' = grad_year <= `year' & exam_time <= grad_time & grad_time != .
}
keep consecutivo ba_???? score_sd yarea_*
reshape long ba_, i(consecutivo) j(year)
egen id = group(consecutivo)
rdrobust ba score_sd, p(1) kernel(epa) all bwselect(mserd) vce(cluster id) covs(yarea_*)

//Appendix Figure C.6 b
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009

foreach year of numlist 2008(1)2016 {
	gen d_earnings`year' = earnings_`year' != .
	replace d_earnings`year' = 1 if ole_formalworker_`year' == 1
}

global K = 2016 - 2008 + 1
mat B = J($K, 7, .)
matrix colnames B = "RD" "SE" "Mean_left" "Observations" "BW_loc_poly" "Effect_obs_left" "Effect_obs_right"
local i = 1
foreach year of numlist 2008(1)2016 {

	rdrobust d_earnings`year' score_sd, p(1) kernel(epa) all bwselect(mserd) vce(cluster year_area) covs(yarea_*)

	mat B[`i', 1] = e(tau_bc)
	mat B[`i', 2] = e(se_tau_rb)
	mat B[`i', 3] = e(tau_bc_l)
	mat B[`i', 4] = e(N)
	mat B[`i', 5] = round(e(h_l), 0.001)
	mat B[`i', 6] = e(N_h_l)
	mat B[`i', 7] = e(N_h_r)

	local i = `i' + 1
}

preserve
clear
svmat B, names(col)
save "Results/Online Appendix/Data/RDFormalWork.dta", replace
restore

//Pooling
//Footnote 48
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009
foreach year of numlist 2008(1)2016 {
	gen d_earnings`year' = earnings_`year' != .
	replace d_earnings`year' = 1 if ole_formalworker_`year' == 1
}
keep consecutivo d_earnings???? score_sd yarea_*
reshape long d_earnings, i(consecutivo) j(year)
egen id = group(consecutivo)
rdrobust d_earnings score_sd, p(1) kernel(epa) all bwselect(mserd) vce(cluster id) covs(yarea_*)

//Main Outcome
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009
tempvar d_earnings 
gen `d_earnings' = ln_earningsMW != .
rdrobust `d_earnings' score_sd, p(1) kernel(epa) all bwselect(mserd) vce(cluster year_area) covs(yarea_*)

/***************************************************************************************************
Appendix Figure C.7: Graduation Rates among Saber Pro Test Takers
****************************************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear

gen N = 1 
collapse (sum) N, by(exam_year grad_year award)
replace grad_year = 0 if grad_year == .
reshape wide N, i(exam_year grad_year) j(award)
reshape wide N1 N0, i(exam_year) j(grad_year)
foreach var of varlist N* {
	replace `var' = 0 if `var' == .
}

egen total = rowtotal(N*)
egen total_awardees = rowtotal(N1*)
foreach year of numlist 2006(1)2016 {
	gen sh`year' = (N0`year' + N1`year')/total
	gen sh_awardees`year' = N1`year'/total_awardees
	if `year' > 2006 {
		local prev_year = `year' - 1
		replace sh`year' = sh`year' + sh`prev_year'
		replace sh_awardees`year' = sh_awardees`year' + sh_awardees`prev_year'
	}
}
keep sh* exam_year
reshape long sh sh_awardees, j(grad_year) i(exam_year)

save "Results/Online Appendix/Data/GraduationRates.dta", replace	

/***************************************************************************************************
Appendix Figure D.1: RD Estimates as Function of the Bandwidth
****************************************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear

local K = 100
local k = 30
mat RDbw = J(`K'-`k'+3, 6, .)
matrix colnames RDbw = "bw" "RD_l" "SE_l" "RD_q" "SE_q" "MSEbw"

//As suggested by Hyytinen et al (2017, QE)
rdrobust $Y $running , $RDoptions bwselect(mserd) p(1) covs($S3)
global Bw_p1 = e(h_l)
global Bias_p1 = e(b_l)
mat RDbw[1, 1] = $Bw_p1
mat RDbw[1, 2] = e(tau_bc)
mat RDbw[1, 3] = e(se_tau_rb)
mat RDbw[1, 6] = 1

rdrobust $Y $running , $RDoptions bwselect(mserd) p(2) covs($S3)
global Bw_p2 = e(h_l)
global Bias_p2 = e(b_l)
mat RDbw[2, 1] = $Bw_p2
mat RDbw[2, 4] = e(tau_bc)
mat RDbw[2, 5] = e(se_tau_rb)		 
mat RDbw[2, 6] = 1

local i = 3
foreach bw of numlist `k'(1)`K' {
	local h = `bw'/100
	mat RDbw[`i', 1] = `h'
		
	//Linear
	rdrobust $Y $running, $RDoptions h(`h') b($Bias_p1) p(1) covs($S3)
	mat RDbw[`i', 2] = e(tau_bc)
	mat RDbw[`i', 3] = e(se_tau_rb)
		
	//Quadratic
	rdrobust $Y $running, $RDoptions h(`h') b($Bias_p2) p(2) covs($S3)
	mat RDbw[`i', 4] = e(tau_bc)
	mat RDbw[`i', 5] = e(se_tau_rb)		 

	mat RDbw[`i', 6] = e(se_tau_rb)
	
local i = `i' + 1
}

preserve
clear
svmat RDbw, names(col)	
save "Results/Online Appendix/Data/BWRobustness.dta", replace
restore

/*********************************************************************************************************
Appendix Figure D.2: Robustness of the Effect of the National Award using Earnings between Ages 23 and 28
**********************************************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y != .

foreach loc_poly in 1 2 { //Order of Polynomial
	foreach bwmethod in mserd cerrd { //Bandwidth Selection Method
	
	//No controls
	if "`bwmethod'" == "mserd" {
		if `loc_poly' == 1 {
			local action "replace"
		}
		else{
			local action ""    
		}

		rdrobust ln_earnings_23_28 $running, kernel($kernel) all bwselect(`bwmethod') p(`loc_poly')
		robustplot, spec(nocovs model`loc_poly' bw`bwmethod') save("Results/Online Appendix/Data/RDRobust_alt") `action' robust
	}

	//Area x Year FE
	rdrobust ln_earnings_23_28 $running , $RDoptions bwselect(`bwmethod') p(`loc_poly') covs($S1)
	robustplot, spec(Areayear model`loc_poly' bw`bwmethod') save("Results/Online Appendix/Data/RDRobust_alt") robust
	
	//FE + Test Scores
	rdrobust ln_earnings_23_28 $running , $RDoptions bwselect(`bwmethod') p(`loc_poly') covs($S2)
	robustplot, spec(Areayear scores model`loc_poly' bw`bwmethod') save("Results/Online Appendix/Data/RDRobust_alt") robust

	//FE + Test Scores + Individual Characteristics
	rdrobust ln_earnings_23_28 $running , $RDoptions bwselect(`bwmethod') p(`loc_poly') covs($S3)
	robustplot, spec(Areayear scores covs model`loc_poly' bw`bwmethod') save("Results/Online Appendix/Data/RDRobust_alt") robust

	//Field x Year FE
	rdrobust ln_earnings_23_28 $running , $RDoptions bwselect(`bwmethod') p(`loc_poly') covs($FFE)
	robustplot, spec(Fieldyear model`loc_poly' bw`bwmethod') save("Results/Online Appendix/Data/RDRobust_alt") robust

	//FE + Test Scores
	rdrobust ln_earnings_23_28 $running , $RDoptions bwselect(`bwmethod') p(`loc_poly') covs($FFE $T)
	robustplot, spec(Fieldyear scores model`loc_poly' bw`bwmethod') save("Results/Online Appendix/Data/RDRobust_alt") robust

	//FE + Test Scores + Individual Characteristics
	rdrobust ln_earnings_23_28 $running , $RDoptions bwselect(`bwmethod') p(`loc_poly') covs($FFE $T $X)
	robustplot, spec(Fieldyear scores covs model`loc_poly' bw`bwmethod') save("Results/Online Appendix/Data/RDRobust_alt") robust
	}
}

/*********************************************************************************************************
Appendix Figure D.3: Robustness of the Effect of the National Distinction on Early-Career Earnings
**********************************************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y != .

unique exam_field
local F = r(unique)
mat F = J(`F', 6, .)
matrix colnames F = "RD" "SE" "Observations" "BW_loc_poly" "Effect_obs_left" "Effect_obs_right"

levelsof exam_field, local(F)
cap drop Field
gen Field = ""
local j = 1
foreach i of local F {
	replace Field = "`i'" in `j'
	
	rdrobust $Y $running if exam_field != "`i'", $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
	mat F[`j', 1] = e(tau_bc)
	mat F[`j', 2] = e(se_tau_rb)
	mat F[`j', 3] = e(N)
	mat F[`j', 4] = round(e(h_l), 0.001)
	mat F[`j', 5] = e(N_h_l)
	mat F[`j', 6] = e(N_h_r)	

local j = `j'+1
}

preserve
keep Field
keep in 1/45
svmat F, names(col)
save "Results/Online Appendix/Data/RDDropFields.dta", replace
restore

/*******************************************************************************
Appendix Figure D.4: Persistence of the Effect on First τ Earnings
********************************************************************************/  
use "Data/Finals/Estimation Sample.dta", clear

local T = 5
mat RDt = J(`T', 5, .)
matrix colnames RDt = "t" "RD_balanced" "SE_balanced" "RD_unbalanced" "SE_unbalanced"
local i = 1
foreach t of numlist 1(1)`T'{
	
	mat RDt[`i', 1] = `t'
	
	//Balanced Sample
	rdrobust ln_earningsMW_tau`t' $running if dynmc_sample == 1, $RDoptions bwselect(mserd) p(1) covs($S3)
	mat RDt[`i', 2] = e(tau_bc)
	mat RDt[`i', 3] = e(se_tau_rb)
	
	//Unbalanced Sample
	rdrobust ln_earningsMW_tau`t' $running, $RDoptions bwselect(mserd) p(1) covs($S3)
	mat RDt[`i', 4] = e(tau_bc)
	mat RDt[`i', 5] = e(se_tau_rb)
	
local i = `i' + 1
}

preserve
clear 
svmat RDt, names(col)
save "Results/Main/Data/RDDynamic_3.dta", replace
restore	

/*******************************************************************************
Appendix Figure F.1: Heterogeneous Effects of the Signal and Earnings Gaps
********************************************************************************/  
//Appendix Figure F.1 a
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009

mat RDhet = J(8, 4, .)
matrix colnames RDhet = "Coeff" "SE" "Variable" "Order"

//Parents' Education
rdrobust $Y $running if parents_cllg == 0, $RDoptions bwselect(mserd) p(1) covs($S2 top5 top20)
mat RDhet[1, 1] = e(tau_bc)
mat RDhet[1, 2] = e(se_tau_rb)
mat RDhet[1, 3] = 1
mat RDhet[1, 4] = 1

rdrobust $Y $running if parents_cllg == 1, $RDoptions bwselect(mserd) p(1) covs($S2 top5 top20)
mat RDhet[2, 1] = e(tau_bc)
mat RDhet[2, 2] = e(se_tau_rb)
mat RDhet[2, 3] = 1
mat RDhet[2, 4] = 2

//Parents' Education
rdrobust $Y $running if parents_bcollar == 1, $RDoptions bwselect(mserd) p(1) covs($S2 top5 top20)
mat RDhet[3, 1] = e(tau_bc)
mat RDhet[3, 2] = e(se_tau_rb)
mat RDhet[3, 3] = 2
mat RDhet[3, 4] = 4

rdrobust $Y $running if parents_bcollar == 0, $RDoptions bwselect(mserd) p(1) covs($S2 top5 top20)
mat RDhet[4, 1] = e(tau_bc)
mat RDhet[4, 2] = e(se_tau_rb)
mat RDhet[4, 3] = 2
mat RDhet[4, 4] = 5

//Network
rdrobust $Y $running if high_network == 0, $RDoptions bwselect(mserd) p(1) covs($S2 top5 top20)
mat RDhet[5, 1] = e(tau_bc)
mat RDhet[5, 2] = e(se_tau_rb)
mat RDhet[5, 3] = 3
mat RDhet[5, 4] = 7

rdrobust $Y $running if high_network == 1, $RDoptions bwselect(mserd) p(1) covs($S2 top5 top20)	
mat RDhet[6, 1] = e(tau_bc)
mat RDhet[6, 2] = e(se_tau_rb)
mat RDhet[6, 3] = 3
mat RDhet[6, 4] = 8

//Gender
rdrobust $Y $running if female == 1, $RDoptions bwselect(mserd) p(1) covs($S2 top5 top20)		
mat RDhet[7, 1] = e(tau_bc)
mat RDhet[7, 2] = e(se_tau_rb)
mat RDhet[7, 3] = 4
mat RDhet[7, 4] = 10

rdrobust $Y $running if female == 0, $RDoptions bwselect(mserd) p(1) covs($S2 top5 top20)
mat RDhet[8, 1] = e(tau_bc)
mat RDhet[8, 2] = e(se_tau_rb)
mat RDhet[8, 3] = 4
mat RDhet[8, 4] = 11

preserve
clear
svmat RDhet, names(col)
gen low95 = Coeff - abs(invnormal(0.025))*SE
gen up95 = Coeff + abs(invnormal(0.025))*SE
save "Results/Main/Data/RDHeterogeneity_cllg_rep_discrete.dta", replace
restore 

//Appendix Figure F.1 b
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009

mat RDGap = J(12, 6, .)
matrix colnames RDGap = "Coeff" "SE" "Order" "Variable" "Category" "df"

//Parents' Education
local sample (parents_cllg==0 & award==1) | (parents_cllg==1 & award==0)
rdrobust $Y $running if `sample', $RDoptions bwselect(mserd) p(1) covs($S2)
global Bw = e(h_l)
mat RDGap[1, 1] = e(tau_bc)
mat RDGap[1, 2] = e(se_tau_rb)
mat RDGap[1, 3] = 1.5
mat RDGap[1, 4] = 1
mat RDGap[1, 5] = 2

tempvar parents_noncllg
gen `parents_noncllg' = parents_cllg == 0 if parents_cllg != .
reg $Y `parents_noncllg' $S2 if award==0 & $running >= -$Bw
mat RDGap[2, 1] = _b[`parents_noncllg']
mat RDGap[2, 2] = _se[`parents_noncllg']
mat RDGap[2, 3] = 1
mat RDGap[2, 4] = 1
mat RDGap[2, 5] = 1
mat RDGap[2, 6] = e(df_r)

reg $Y `parents_noncllg' $S2 if award==1 & $running <= $Bw
mat RDGap[3, 1] = _b[`parents_noncllg']
mat RDGap[3, 2] = _se[`parents_noncllg']
mat RDGap[3, 3] = 2
mat RDGap[3, 4] = 1
mat RDGap[3, 5] = 3
mat RDGap[3, 6] = e(df_r)

//Parents' Occupation
local sample (parents_bcollar==1 & award==1) | (parents_bcollar==0 & award==0)
rdrobust $Y $running if `sample', $RDoptions bwselect(mserd) p(1) covs($S2)
global Bw = e(h_l)
mat RDGap[4, 1] = e(tau_bc)
mat RDGap[4, 2] = e(se_tau_rb)
mat RDGap[4, 3] = 4.5
mat RDGap[4, 4] = 2
mat RDGap[4, 5] = 2

reg $Y parents_bcollar $S2 if award==0 & $running >= -$Bw
mat RDGap[5, 1] = _b[parents_bcollar]
mat RDGap[5, 2] = _se[parents_bcollar]
mat RDGap[5, 3] = 4
mat RDGap[5, 4] = 2
mat RDGap[5, 5] = 1
mat RDGap[5, 6] = e(df_r)

reg $Y parents_bcollar $S2 if award==1 & $running <= $Bw
mat RDGap[6, 1] = _b[parents_bcollar]
mat RDGap[6, 2] = _se[parents_bcollar]
mat RDGap[6, 3] = 5
mat RDGap[6, 4] = 2
mat RDGap[6, 5] = 3
mat RDGap[6, 6] = e(df_r)

//Networks
tempvar low_network 
gen `low_network' = high_network == 0 if high_network != .
local sample (`low_network'==1 & award==1) | (`low_network'==0 & award==0)
rdrobust $Y $running if `sample', $RDoptions bwselect(mserd) p(1) covs($S2)
global Bw = e(h_l)
mat RDGap[7, 1] = e(tau_bc)
mat RDGap[7, 2] = e(se_tau_rb)
mat RDGap[7, 3] = 7.5
mat RDGap[7, 4] = 3
mat RDGap[7, 5] = 2

reg $Y `low_network' $S2 if award==0 & $running >= -$Bw
mat RDGap[8, 1] = _b[`low_network']
mat RDGap[8, 2] = _se[`low_network']
mat RDGap[8, 3] = 7
mat RDGap[8, 4] = 3
mat RDGap[8, 5] = 1
mat RDGap[8, 6] = e(df_r)

reg $Y `low_network' $S2 if award==1 & $running <= $Bw
mat RDGap[9, 1] = _b[`low_network']
mat RDGap[9, 2] = _se[`low_network']
mat RDGap[9, 3] = 8
mat RDGap[9, 4] = 3
mat RDGap[9, 5] = 3
mat RDGap[9, 6] = e(df_r)

//Gender
local sample (female==1 & award==1) | (female==0 & award==0)
rdrobust $Y $running if `sample', $RDoptions bwselect(mserd) p(1) covs($S2)
global Bw = e(h_l)
mat RDGap[10, 1] = e(tau_bc)
mat RDGap[10, 2] = e(se_tau_rb)
mat RDGap[10, 3] = 10.5
mat RDGap[10, 4] = 4
mat RDGap[10, 5] = 2

reg $Y female $S2 if award==0 & $running >= -$Bw
mat RDGap[11, 1] = _b[female]
mat RDGap[11, 2] = _se[female]
mat RDGap[11, 3] = 10
mat RDGap[11, 4] = 4 
mat RDGap[11, 5] = 1
mat RDGap[11, 6] = e(df_r)

reg $Y female $S2 if award==1 & $running <= $Bw
mat RDGap[12, 1] = _b[female]
mat RDGap[12, 2] = _se[female]
mat RDGap[12, 3] = 11
mat RDGap[12, 4] = 4
mat RDGap[12, 5] = 3
mat RDGap[12, 6] = e(df_r)

preserve
clear
svmat RDGap, names(col)
gen low95 = Coeff - abs(invnormal(0.025))*SE if Category == 2
gen up95 = Coeff + abs(invnormal(0.025))*SE if Category == 2
replace low95 = Coeff - abs(invt(df, 0.025))*SE if Category != 2
replace up95 = Coeff + abs(invt(df, 0.025))*SE if Category != 2
save "Results/Online Appendix/Data/Gaps.dta", replace
restore


/*******************************************************************************
Appendix Tables
********************************************************************************/

/*******************************************************************************
Appendix Table A.1: Eligibility to Take the College Exit Exam by Cohort
********************************************************************************/  
use "Data\Finals\EnrollmentRecords_CollegeExam.dta", clear

//Year of Enrollment 
tostring prim_sem, gen(prim_year)
replace prim_year = substr(prim_year, 1, 4)
destring prim_year, replace

//Year of Enrollment 
tostring grado_per, gen(grad_year)
replace grad_year = substr(grad_year, 1, 4)
destring grad_year, replace

//Eligibility Criteria:
// Restrict to cohorts 2002 to 2007
keep if prim_year >= 2002 & prim_year <= 2007
// Keep students who graduated between 2006 and 2012 (or took the exam)
keep if grad_year >= 2006 & grad_year <= 2012
// Drop if document is "Tarjeta de Identidad"
drop if doc_tipo == "T"

replace spa_spro = 0 if exam_year > grad_year
replace exam_year = . if spa_spro == 0

//Drop fields without ECAES 2006 to 2009
drop if regex(program_field, "ARTE") | regex(program_field, "PLASTICAS")
drop if regex(program_field, "MILITAR")
drop if regex(program_field, "POLITICA")
drop if regex(program_field, "FILOSOFIA")
drop if regex(program_field, "BIOMEDICA")
drop if regex(program_field, "BIBLIOTECOLOGIA")
drop if program_field == "SALUD PUBLICA"
drop if program_field == "MUSICA"
drop if program_field == "DISENIO"
drop if program_field == "SIN CLASIFICAR"
drop if program_field == "OTRAS INGENIERIAS"

//College Information
preserve
use "Data\Originals\SNIES\Enrollment 2000-2019.dta", clear
duplicates drop cllg_code, force
keep cllg_code cllg_sector cllg_type cllg_muni_code
rename cllg_code ies
tempfile CllgInfo
save `CllgInfo', replace
restore 

merge m:1 ies using `CllgInfo'
keep if _merge == 3
drop _merge

//College Ranking
cap drop univ*
gen univ_1 = (ies==1101) //UNal
gen univ_2 = (ies == 1201) //Universidad de Antioquia
gen univ_3 = (ies == 1203) //Universidad del Valle
gen univ_4 = (ies == 1813) //Universidad de Los Andes
gen univ_5 = (ies == 1701) //Universidad Javeriana

gen univ_6 = (ies==1102) //UNal*
gen univ_7 = (ies == 1204) //Universidad Industrial de Santander
gen univ_8 = (ies == 1713) //Universidad del Norte
gen univ_9 = (ies == 1110) //Universidad del Cauca
gen univ_10 = (ies == 2711) //Universidad Tecnologica de Pereira*
gen univ_11 = (ies == 1112) //Universidad de Caldas
gen univ_12 = (ies == 1710) //Universidad Pontificia Bolivariana
gen univ_13 = (ies == 1106) //Universidad Tecnologica y Pedagogica*
gen univ_14 = (ies == 1205) //Universidad de Cartagena*
gen univ_15 = (ies == 1706) //Universidad Externado de Colombia
gen univ_16 = (ies == 1714) //Universidad del Rosario
gen univ_17 = (ies == 1301) //Universidad Distrital
gen univ_18 = (ies == 1704) //Universidad Santo Tomas*
gen univ_19 = (ies == 1712) //Universidad EAFIT
gen univ_20 = (ies == 1711) //Universidad de La Sabana

cap drop top5 top20 above20
gen top5 = 0
gen top20 = 0
foreach i of numlist 1(1)20 {
if `i' <= 5 {
replace top5 = 1 if univ_`i' == 1
}
if `i' >= 6 & `i' <= 20 {
replace top20 = 1 if univ_`i' == 1
}
}
gen above20 = (top5==0 & top20==0)

//Age at Enrollment
destring nac_ano, replace
gen age = prim_year - nac_ano
replace age = prim_year - year(spro_birthdate) if age == .
replace age = . if age < 15 | age > 45

//College Sector and Type
gen cllg_prv = cllg_sector == "PRIVADA"

//Socioeconomic Stratum 
gen d_SES = estrato == .
bys program_code prim_sem: egen aux = mean(estrato)
replace estrato = round(aux, 1) if estrato == . 
drop aux

gen high_SES = estrato >= 3 if estrato != .

//Pass Rate
sum materias_tomadas, d
replace materias_tomadas = . if materias_tomadas < r(p5)
replace materias_tomadas = . if materias_tomadas > 200

gen pass_rate = round(materias_aprobadas/materias_tomadas, 0.001)

gen d_pass_rate = pass_rate == .
bys program_code prim_sem: egen aux = mean(pass_rate)
replace pass_rate = round(aux, 0.001) if pass_rate == .
drop aux

//High School Exam Score
gen d_puntaje_icfes = puntaje_icfes == .
bys program_code prim_sem: egen aux = mean(puntaje_icfes)
replace puntaje_icfes = round(aux, 1) if puntaje_icfes == .
drop aux

//Gender
rename sexo female

//Sample Selection
drop if age == .
drop if female == .
drop if puntaje_icfes == .
drop if estrato == .

//Drop programs in which less than 5% take the exam
bys program_code : egen aux = mean(spa_spro)
drop if aux < 0.05 //4,630 obs
drop aux

save "Data\Finals\SProEligible.dta", replace

/*******************************************************************************
Appendix Table A.2: Probability of Taking the College Exit Exam
********************************************************************************/  
use "Data\Finals\EnrollmentRecords_CollegeExam.dta", clear

//Year of Enrollment 
tostring prim_sem, gen(prim_year)
replace prim_year = substr(prim_year, 1, 4)
destring prim_year, replace

//Year of Enrollment 
tostring grado_per, gen(grad_year)
replace grad_year = substr(grad_year, 1, 4)
destring grad_year, replace

//Eligibility Criteria:
// Restrict to cohorts 2002 to 2007
keep if prim_year >= 2002 & prim_year <= 2007
// Keep students who graduated between 2006 and 2012 (or took the exam)
keep if grad_year >= 2006 & grad_year <= 2012
// Drop if document is "Tarjeta de Identidad"
drop if doc_tipo == "T"

replace spa_spro = 0 if exam_year > grad_year
replace exam_year = . if spa_spro == 0

//Drop fields without ECAES 2006 to 2009
drop if regex(program_field, "ARTE") | regex(program_field, "PLASTICAS")
drop if regex(program_field, "MILITAR")
drop if regex(program_field, "POLITICA")
drop if regex(program_field, "FILOSOFIA")
drop if regex(program_field, "BIOMEDICA")
drop if regex(program_field, "BIBLIOTECOLOGIA")
drop if program_field == "SALUD PUBLICA"
drop if program_field == "MUSICA"
drop if program_field == "DISENIO"
drop if program_field == "SIN CLASIFICAR"
drop if program_field == "OTRAS INGENIERIAS"

//College Information
preserve
use "Data\Originals\SNIES\Enrollment 2000-2019.dta", clear
duplicates drop cllg_code, force
keep cllg_code cllg_sector cllg_type cllg_muni_code
rename cllg_code ies
tempfile CllgInfo
save `CllgInfo', replace
restore 

merge m:1 ies using `CllgInfo'
keep if _merge == 3
drop _merge

//College Ranking
cap drop univ*
gen univ_1 = (ies==1101) //UNal
gen univ_2 = (ies == 1201) //Universidad de Antioquia
gen univ_3 = (ies == 1203) //Universidad del Valle
gen univ_4 = (ies == 1813) //Universidad de Los Andes
gen univ_5 = (ies == 1701) //Universidad Javeriana

gen univ_6 = (ies==1102) //UNal*
gen univ_7 = (ies == 1204) //Universidad Industrial de Santander
gen univ_8 = (ies == 1713) //Universidad del Norte
gen univ_9 = (ies == 1110) //Universidad del Cauca
gen univ_10 = (ies == 2711) //Universidad Tecnologica de Pereira*
gen univ_11 = (ies == 1112) //Universidad de Caldas
gen univ_12 = (ies == 1710) //Universidad Pontificia Bolivariana
gen univ_13 = (ies == 1106) //Universidad Tecnologica y Pedagogica*
gen univ_14 = (ies == 1205) //Universidad de Cartagena*
gen univ_15 = (ies == 1706) //Universidad Externado de Colombia
gen univ_16 = (ies == 1714) //Universidad del Rosario
gen univ_17 = (ies == 1301) //Universidad Distrital
gen univ_18 = (ies == 1704) //Universidad Santo Tomas*
gen univ_19 = (ies == 1712) //Universidad EAFIT
gen univ_20 = (ies == 1711) //Universidad de La Sabana

cap drop top5 top20 above20
gen top5 = 0
gen top20 = 0
foreach i of numlist 1(1)20 {
if `i' <= 5 {
replace top5 = 1 if univ_`i' == 1
}
if `i' >= 6 & `i' <= 20 {
replace top20 = 1 if univ_`i' == 1
}
}
gen above20 = (top5==0 & top20==0)

//Age at Enrollment
destring nac_ano, replace
gen age = prim_year - nac_ano
replace age = prim_year - year(spro_birthdate) if age == .
replace age = . if age < 15 | age > 45

//College Sector and Type
gen cllg_prv = cllg_sector == "PRIVADA"

//Socioeconomic Stratum 
gen d_SES = estrato == .
bys program_code prim_sem: egen aux = mean(estrato)
replace estrato = round(aux, 1) if estrato == . 
drop aux

gen high_SES = estrato >= 3 if estrato != .

//Pass Rate
sum materias_tomadas, d
replace materias_tomadas = . if materias_tomadas < r(p5)
replace materias_tomadas = . if materias_tomadas > 200

gen pass_rate = round(materias_aprobadas/materias_tomadas, 0.001)

gen d_pass_rate = pass_rate == .
bys program_code prim_sem: egen aux = mean(pass_rate)
replace pass_rate = round(aux, 0.001) if pass_rate == .
drop aux

//High School Exam Score
gen d_puntaje_icfes = puntaje_icfes == .
bys program_code prim_sem: egen aux = mean(puntaje_icfes)
replace puntaje_icfes = round(aux, 1) if puntaje_icfes == .
drop aux

//Gender
rename sexo female

//Sample Selection
drop if age == .
drop if female == .
drop if puntaje_icfes == .
drop if estrato == .

//Drop programs in which less than 5% take the exam
bys program_code : egen aux = mean(spa_spro)
drop if aux < 0.05 //4,630 obs
drop aux

save "Data\Finals\SProEligible.dta", replace
use "Data\Finals\SProEligible.dta", clear

reg spa_spro puntaje_icfes d_puntaje_icfes, cluster(ies)
outreg2 using "Results/Online Appendix/Data/ProbSaberPro.txt", dec(5) dta bra replace nonotes nocons

reg spa_spro puntaje_icfes top5 top20 d_puntaje_icfes, cluster(ies)
outreg2 using "Results/Online Appendix/Data/ProbSaberPro.txt", dec(5) dta bra append nonotes nocons 

reg spa_spro puntaje_icfes top5 top20 cllg_prv d_puntaje_icfes, cluster(ies)
outreg2 using "Results/Online Appendix/Data/ProbSaberPro.txt", dec(5) dta bra append nonotes nocons 

reghdfe spa_spro puntaje_icfes female age high_SES d_puntaje_icfes d_SES, cluster(ies) absorb(prim_sem program_field) keepsingletons
outreg2 using "Results/Online Appendix/Data/ProbSaberPro.txt", dec(5) dta bra append nonotes nocons ///
addtext("Cohort FE", "Yes", "Field of Study FE", "Yes")

reghdfe spa_spro puntaje_icfes top5 top20 female age high_SES d_puntaje_icfes d_SES, cluster(ies) absorb(prim_sem program_field) keepsingletons
outreg2 using "Results/Online Appendix/Data/ProbSaberPro.txt", dec(5) dta bra append nonotes nocons  ///
addtext("Cohort FE", "Yes", "Field of Study FE", "Yes")

reghdfe spa_spro puntaje_icfes top5 top20 cllg_prv female age high_SES d_puntaje_icfes d_SES, cluster(ies) absorb(prim_sem program_field) keepsingletons
outreg2 using "Results/Online Appendix/Data/ProbSaberPro.txt", dec(5) dta bra append nonotes nocons  ///
addtext("Cohort FE", "Yes", "Field of Study FE", "Yes")

/*******************************************************************************
Appendix Table A.3: Description of Students Around Threshold by Field-Specific Exam
********************************************************************************/  
preserve
use "Data/Finals/Estimation Sample.dta", clear

tab award if abs(score_sd) < abs($MSE_Bw)
gen AroundT = (score_sd>0) if abs(score_sd) < abs($MSE_Bw)
gen above = (score_sd>=0)
gen below = score_sd<0

gen around_=1 
collapse (sum) around_ below above, by(exam_field area AroundT)
decode area, gen(exam_area)
drop area
replace AroundT = 2 if AroundT == .
reshape wide around_ below above, i(exam_field exam_area) j(AroundT)

order exam_field exam_area around_? below? above?

egen below = rowtotal(below*)
drop below?
egen above = rowtotal(above*)
drop above?
drop around_2

gen total = below + above

replace exam_area = subinstr(exam_area, "Sciences", "Sc.", .)
replace exam_area = "Social Sc." if exam_area == "Social Sc."
replace exam_area = "Business \& Econ" if regexm(exam_area, "Econ")
sort exam_area exam_field

order exam_field exam_area total below above around_0 around_1
tostring total below above around_0 around_1, replace format(%9.0fc) force

// Export
# delimit ;	
texsave exam_field exam_area total above around_1 around_0 using "Results/Online Appendix/ObsByField.tex",
headerlines("
\multicolumn{1}{c}{\multirow{2}{*}{Exam Field}} & \multirow{2}{*}{Area} & \multirow{2}{*}{Test Takers} & \multirow{2}{*}{Awardees} & \multicolumn{2}{c}{MSE-Bandwidth} \\\cline{5-6} \addlinespace
 & & & & Awardees & Non-Awardees \\\cline{1-1}\cline{2-2}\cline{3-3}\cline{4-4}\cline{5-5}\cline{6-6}
 & & (1) & (2) & (3) & (4) \\\addlinespace
") 
replace align(l@{\extracolsep{4pt}}ccccc) location(h) frag nonames nofix  
; # delimit cr
tex_edits, file("Results/Online Appendix/ObsByField.tex")
restore

/*******************************************************************************
Appendix Table A.4: Description of Estimation Sample by College Ranking
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear

keep if exam_year <= 2009
gen catranking = top5
replace catranking = 2 if top20==1
replace catranking = 3 if above20==1

gen one = 1
egen total = sum(one), by(cllg_code)
gen double w_cllg = one/total
drop one total 

// Compute Statistics

tab cllg_prv catranking [iw=w_cllg], m matcell(mat1)

mat mat2 = J(1,3,.)
foreach  i of numlist 1(1)3 {
unique exam_id if catranking == `i'
mat mat2[1, `i'] = 	r(unique)
}
unique exam_id
local F = r(unique)

tab exam_year catranking, matcell(mat3)

tab cllg_prv catranking if award==1, matcell(mat4)
	
// Create Matrices
mat M = J(1,3,.) \mat1 \ mat2 \ J(1,3,.) \  mat3 \ J(1,3,.) \ mat4
clear
svmat M
// Edit Variables
gen 	names = "\addlinespace\hspace{5pt} Public Universities"  in 2
replace names = "\hspace{5pt} Private Universities" in 3
replace names = "\hspace{5pt} Field Exams"  in 4
replace names = "\addlinespace\hspace{5pt} 2006"  in 6
replace names = "\hspace{5pt} 2007"  in 7
replace names = "\hspace{5pt} 2008"  in 8
replace names = "\hspace{5pt} 2009"  in 9
replace names = "\addlinespace\hspace{5pt} Public  University"  in 11
replace names = "\hspace{5pt} Private University"  in 12

replace names =  "\textit{Number of:}"   in 1
replace names =  "\addlinespace\textit{Number of Test Takers per Year:}" in 5
replace names =  "\addlinespace\textit{Number of Awardees:}"    in 10
	
egen M4 = rowtotal(M1 M2 M3) if names!=""
replace M4 = `F' in 4
replace M4 = . if M4 == 0
	
br names M1-M4
tostring M1-M4, replace format(%9.0fc) force
forvalues i = 1/4 {
	replace M`i' = "" if M`i' =="." 
}

order names M1 M2 M3 M4

// Export
# delimit ;	
texsave names M1 M2 M3 M4 using "Results/Online Appendix/DescCllg.tex",
headerlines("
& Top 5 & Top 6-20 & Above 20 & Total \\\cline{2-2}\cline{3-3}\cline{4-4}\cline{5-5}\addlinespace
& (1) & (2) & (3) & (4)
") 
replace align(l@{\extracolsep{4pt}}cccc) location(h) frag nonames nofix  
; # delimit cr
tex_edits, file("Results/Online Appendix/DescCllg.tex")

/*******************************************************************************
Appendix Table A.5: Awardee Characteristics by Area
********************************************************************************/
preserve
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009
keep if award == 1

gen fst_gen_college_student  = meduc_cllg == 0 & meduc_grdsch == 0 & feduc_cllg == 0 & feduc_grdsch == 0

global X2 female age high_network low_SES fst_gen_college_student meduc_cllg feduc_cllg working cllg_prv top5 top20 above20 num_semesters sb11score reading english 

tabstat $X2, save
mat A0 = r(StatTotal)'
forvalues i = 1/6{
	tabstat $X2 if area`i' == 1, save
	mat A`i' = r(StatTotal)'
}

mat A = A0, A1, A2, A3, A4, A5, A6
matlist A

clear
svmat A
save "Results/Online Appendix/Data/AwardeeCharacteristics.dta", replace
restore

/*******************************************************************************
Appendix Table B.1: Description of the Estimation Sample
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009
keep if sample == 1 | sample >= 9

tab exam_id sample, matcell(F)
matrix colnames F = "fields1" "fields9" "fields10"

gen grad = grad_year != .
gen earnings = $Y != .

unique exam_id if grad == 1 & sample == 1
local F_grad = r(unique)
unique exam_id if earnings == 1 & sample == 1
local F_earnings = r(unique)

count if grad == 1 & sample == 1
local S_grad = r(N)
count if earnings == 1 & sample == 1
local S_earnings = r(N)

count if grad == 1 & award == 1 & sample == 1
local A_grad = r(N)
count if earnings == 1 & award == 1 & sample == 1
local A_earnings = r(N)

count if sample == 1
local S = r(N)
local S_grad_p = round(`S_grad'*100/`S', 0.01)
local S_grad_p = substr("`S_grad_p'", 1, 4)
local S_earnings_p = round(`S_earnings'*100/`S_grad', 0.01)
local S_earnings_p = substr("`S_earnings_p'", 1, 4)

count if sample == 1 & award == 1
local A = r(N)
local A_grad_p = round(`A_grad'*100/`A', 0.01)
local A_grad_p = substr("`A_grad_p'", 1, 4)
local A_earnings_p = round(`A_earnings'*100/`A_grad', 0.01)
local A_earnings_p = substr("`A_earnings_p'", 1, 4)

//Students
gen N = 1
collapse (sum) N award, by(sample)

//Fields
preserve
clear 
svmat F, names(col)
foreach var of varlist fields* {
    replace `var' = 1 if `var' > 0
}
collapse (sum) fields*
gen i = _n
reshape long fields, i(i) j(sample)
drop i
tempfile Fields
save `Fields', replace
restore

merge 1:1 sample using `Fields'
drop _merge

tostring sample, replace

count 
local N = r(N)+2
set obs `N'
	
egen aux1 = total(N)
egen aux2 = total(award)
egen aux3 = total(fields) if sample != "9"
replace N = aux1 in `N'
replace award = aux2 in `N'
replace fields = aux3 in `N'
drop aux*

gen i = _n
replace i = 0 if i == `N'
replace i = 0.5 if i == `N'-1
replace i = `N' if i == 1
sort i
drop i

count 
local N = r(N)+2
set obs `N'
local N1 = `N'-1

replace fields = `F_grad' in `N1'
replace fields = `F_earnings' in `N'
replace N = `S_grad' in `N1'
replace N = `S_earnings' in `N'
replace award = `A_grad' in `N1'
replace award = `A_earnings' in `N'

tostring N award fields, format(%9.0fc) replace force
replace sample = "\addlinespace Universe Under Analysis" in 1
replace sample = "\addlinespace \textit{Fields with Missing Running Variable}" in 2
replace sample = "\addlinespace \hspace{8pt} Some Cohorts" if sample == "9"
replace sample = "\hspace{8pt} All Cohorts" if sample == "10"
replace sample = "\addlinespace Sample of Analysis" if sample == "1"
replace sample = "\addlinespace College Graduates" in 6
replace sample = " Graduates with Earnings" in 7

replace N = N + " (`S_grad_p'\%)" in `N1'
replace N = N + " (`S_earnings_p'\%)" in `N'
replace award = award + " (`A_grad_p'\%)" in `N1'
replace award = award + " (`A_earnings_p'\%)" in `N'

foreach var of varlist _all {
    replace `var' = "" if `var' == "."
}

order sample fields N award

// Export
# delimit ;	
texsave  using "Results/Online Appendix/SampleDescription.tex",
headerlines("
& Fields & Test Takers & Awardees \\\cline{2-2}\cline{3-3}\cline{4-4}\addlinespace
& (1) & (2) & (3)
") 
replace align(l@{\extracolsep{4pt}}cccc) location(h) frag nonames nofix  
; # delimit cr
tex_edits, file("Results/Online Appendix/SampleDescription.tex")


/*******************************************************************************
Appendix Table B.2: Summary Statistics of College Exit Exam Test-Takers, 2006-2009
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y != .

//Firms Sample
cap drop firms_sample
gen firms_sample = 1 if !missing(AKMRank_avg, AKMRank_fst, CIIURank_avg, CIIURank_fst)

//Industries Sample
cap drop ciiu_sample
gen ciiu_sample = 1 if !missing(CIIUMatch2_avg, CIIUMatch1_avg, CIIUMatch1_fst, CIIUMatch2_fst, CIIUMatch1_ever, CIIUMatch2_ever)

tempvar main ciiu firm
gen `main' = 1 if $Y != .
gen `ciiu' = 1 if ciiu_sample == 1
gen `firm' = 1 if firms_sample == 1

global Cov1 female age stratum meduc_hs meduc_cllg meduc_grdsch
global Cov2 cllg_prv top5 top20 
global Cov3 area1 area2 area3 area4 area5 area6
global Variables award $Cov1 $Cov2 $Cov3

tabstat $Variables if `main' == 1, s(mean sd N) c(s) save
mat A = r(StatTotal)'
tabstat $Variables if `ciiu' == 1, s(mean sd N) c(s) save
mat B = r(StatTotal)'
tabstat $Variables if `firm' == 1, s(mean sd N) c(s) save
mat C = r(StatTotal)'

preserve
	clear
	global K = `: word count $Variables' 
	set obs $K
	gen variable = ""
	local i = 1
	foreach var in $Variables {
		replace variable = "`var'" in `i'
		local i = `i'+1
	}

	svmat A
	svmat B
	svmat C
	save "Results/Online Appendix/Data/Statistics_dta", replace 
restore

/*******************************************************************************
Appendix Table C.1: Months from College Exit Exam and Graduation Date to First Observed Earnings
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y != .

tempvar months_exam_to_earnings months_grad_to_earnings
gen `months_exam_to_earnings' = ln(months_exam_to_earningsMW)
gen `months_grad_to_earnings' = ln(months_grad_to_earningsMW)

//Months from Exam Date to Observe Earnings
rdrobust `months_exam_to_earnings' $running, $RDoptions bwselect(mserd) p(1) covs($S1)
global Bw = e(h_l)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum `months_exam_to_earnings' if award == 0 & abs($running) <= $Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/MonthsToEarnings.txt", dec(3) bra dta lab replace nonotes ///
adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu) cti(" ") ///
addtext("Area x Year FE", "Yes", "Test Scores", "No", "Covariates", "No")

rdrobust `months_exam_to_earnings' $running, $RDoptions bwselect(mserd) p(1) covs($S2)
global Bw = e(h_l)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum `months_exam_to_earnings' if award == 0 & abs($running) <= $Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/MonthsToEarnings.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu) cti(" ") ///
addtext("Area x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "No")

rdrobust `months_exam_to_earnings' $running, $RDoptions bwselect(mserd) p(1) covs($S3)
global Bw = e(h_l)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum `months_exam_to_earnings' if award == 0 & abs($running) <= $Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/MonthsToEarnings.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu) cti(" ") ///
addtext("Area x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "Yes")

//Months from Graduation Date to Observe Earnings
rdrobust `months_grad_to_earnings' $running, $RDoptions bwselect(mserd) p(1) covs($S1)
global Bw = e(h_l)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum `months_grad_to_earnings' if award == 0 & abs($running) <= $Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/MonthsToEarnings.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu) cti(" ") ///
addtext("Area x Year FE", "Yes", "Test Scores", "No", "Covariates", "No")

rdrobust `months_grad_to_earnings' $running, $RDoptions bwselect(mserd) p(1) covs($S2)
global Bw = e(h_l)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum `months_grad_to_earnings' if award == 0 & abs($running) <= $Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/MonthsToEarnings.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu) cti(" ") ///
addtext("Area x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "No")

rdrobust `months_grad_to_earnings' $running, $RDoptions bwselect(mserd) p(1) covs($S3)
global Bw = e(h_l)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum `months_grad_to_earnings' if award == 0 & abs($running) <= $Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/MonthsToEarnings.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu) cti(" ") ///
addtext("Area x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "Yes")

/*******************************************************************************
Appendix Table D.1: Effect of the National Award on Different Measures of Early-Career Earnings
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y != .

local i = 1
foreach y of varlist ln_earningsMW ln_earningsMW_1 ln_earningsMW_23_28 {

	rdrobust `y' $running , $RDoptions bwselect(mserd) p(1) covs($S1)
	global Bw = e(h_l)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	outreg2 using "Results/Online Appendix/Data/RDEarnings_`i'.txt", dec(3) bra dta lab replace nonotes ///
	adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
	addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "No", "Covariates", "No")

	rdrobust `y' $running , $RDoptions bwselect(mserd) p(1) covs($S2)
	global Bw = e(h_l)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	outreg2 using "Results/Online Appendix/Data/RDEarnings_`i'.txt", dec(3) bra dta lab append nonotes ///
	adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
	addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "No")

	rdrobust `y' $running , $RDoptions bwselect(mserd) p(1) covs($S3)
	global Bw = e(h_l)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	outreg2 using "Results/Online Appendix/Data/RDEarnings_`i'.txt", dec(3) bra dta lab append nonotes ///
	adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
	addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "Yes")

	//Field x Year FE
	rdrobust `y' $running , $RDoptions bwselect(mserd) p(1) covs($FFE)
	global Bw = e(h_l)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	outreg2 using "Results/Online Appendix/Data/RDEarnings_`i'.txt", dec(3) bra dta lab append nonotes ///
	adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
	addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "No", "Covariates", "No")

	rdrobust `y' $running , $RDoptions bwselect(mserd) p(1) covs($FFE $T)
	global Bw = e(h_l)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	outreg2 using "Results/Online Appendix/Data/RDEarnings_`i'.txt", dec(3) bra dta lab append nonotes ///
	adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
	addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "No")

	rdrobust `y' $running , $RDoptions bwselect(mserd) p(1) covs($FFE $T $X)
	global Bw = e(h_l)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	outreg2 using "Results/Online Appendix/Data/RDEarnings_`i'.txt", dec(3) bra dta lab append nonotes ///
	adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
	addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "Yes")
	
	if `i' == 1 {
		preserve
		use "Results/Online Appendix/Data/RDEarnings_`i'_dta", clear
		save "Results/Online Appendix/Data/RDRobust.dta", replace
		restore	
	}
	if `i' > 1 {
		preserve
		use "Results/Online Appendix/Data/RDRobust.dta", clear
		append using "Results/Online Appendix/Data/RDEarnings_`i'_dta"
		save "Results/Online Appendix/Data/RDRobust.dta", replace
		restore
	}
	erase "Results/Online Appendix/Data/RDEarnings_`i'_dta.dta"
	erase "Results/Online Appendix/Data/RDEarnings_`i'.txt"
	
local i = `i' + 1
}

/***************************************************************************************************
Appendix Table D.2: Robustness: Effect of National Distinction Award on Early-Career Earnings, Excluding 2006 Test-Takers
****************************************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 

rdrobust $Y $running if exam_year != 2006, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S1)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
outreg2 using "Results/Online Appendix/Data/RDMain_No2006.txt", dec(3) bra lab replace dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "No", "Covariates", "No") 

rdrobust $Y $running if exam_year != 2006, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S2)
outreg2 using "Results/Online Appendix/Data/RDMain_No2006.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "No") 

rdrobust $Y $running if exam_year != 2006, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
outreg2 using "Results/Online Appendix/Data/RDMain_No2006.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "Yes") 

//Field x Year FE
rdrobust $Y $running if exam_year != 2006, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE)
outreg2 using "Results/Online Appendix/Data/RDMain_No2006.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "No", "Covariates", "No") 

rdrobust $Y $running if exam_year != 2006, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE $T)
outreg2 using "Results/Online Appendix/Data/RDMain_No2006.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "No") 

rdrobust $Y $running if exam_year != 2006, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE $T $X)
outreg2 using "Results/Online Appendix/Data/RDMain_No2006.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "Yes") 

/***************************************************************************************************
Appendix Table D.3: Robustness of the Effect of the National Distinction on Early-Career Earnings
****************************************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear

keep if exam_year <= 2009 
local N = 1000
bys exam_field : egen field_testtakers = count(exam_field)
local field field_testtakers
tab exam_field if `field' < `N'

rdrobust $Y $running if `field' >= `N', $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S1)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
outreg2 using "Results/Online Appendix/Data/RDNoSmallFields.txt", dec(3) bra lab replace dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "No", "Covariates", "No") 

rdrobust $Y $running if `field' >= `N', $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S2)
outreg2 using "Results/Online Appendix/Data/RDNoSmallFields.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "No") 

rdrobust $Y $running if `field' >= `N', $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
outreg2 using "Results/Online Appendix/Data/RDNoSmallFields.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "Yes") 

//Field x Year FE
rdrobust $Y $running if `field' >= `N', $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE)
outreg2 using "Results/Online Appendix/Data/RDNoSmallFields.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "No", "Covariates", "No") 

rdrobust $Y $running if `field' >= `N', $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE $T)
outreg2 using "Results/Online Appendix/Data/RDNoSmallFields.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "No") 

rdrobust $Y $running if `field' >= `N', $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE $T $X)
outreg2 using "Results/Online Appendix/Data/RDNoSmallFields.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "Yes") 

/***************************************************************************************************
Appendix Table D.4: Robustness: Effect of National Distinction Award on Early-Career Earnings, Excluding Fields with less than 10 Awards
****************************************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear

keep if exam_year <= 2009 & $Y!=.
bys fieldtest_time: egen award_per_field = sum(award)
//Area of Study x Year FE
rdrobust $Y $running if award_per_field >= 10, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S1)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
outreg2 using "Results/Online Appendix/Data/RDMain_10APF.txt", dec(3) bra lab replace dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "No", "Covariates", "No") 

rdrobust $Y $running if award_per_field >= 10, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S2)
outreg2 using "Results/Online Appendix/Data/RDMain_10APF.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "No") 

rdrobust $Y $running if award_per_field >= 10, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
outreg2 using "Results/Online Appendix/Data/RDMain_10APF.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "Yes") 

//Field x Year FE
rdrobust $Y $running if award_per_field >= 10, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE)
outreg2 using "Results/Online Appendix/Data/RDMain_10APF.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "No", "Covariates", "No") 

rdrobust $Y $running if award_per_field >= 10, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE $T)
outreg2 using "Results/Online Appendix/Data/RDMain_10APF.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "No") 

rdrobust $Y $running if award_per_field >= 10, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE $T $X)
outreg2 using "Results/Online Appendix/Data/RDMain_10APF.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "Yes") 

/***************************************************************************************************
Appendix Table D.5: Effect of the National Distinction Award on Earnings Using the Ranking of Scores as Running Variable
****************************************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y!=.

replace rank_norm = rank_norm + 1 if rank_norm >=0 
tab rank_norm if abs(rank_norm) <= 1
gen inter=rank_norm*award

//Area x Year FE
foreach h of numlist 1(2)7 {

	local covs award rank_norm inter $X $T
	local action append
	if `h' == 1 {
		local covs award 
		local action replace
	}

	reghdfe $Y `covs' if abs(rank_norm)<=`h', absorb(year_area) cluster(year_area) keepsingletons
	count if abs(rank_norm)<=`h' & award == 1
	global Nr = r(N)
	count if abs(rank_norm)<=`h' & award == 0
	global Nl = r(N)
	outreg2 using "Results/Online Appendix/Data/RDRank.txt",dec(3) dta bra `action' nonotes nocons drop($X $T) ///
	addtext("Bandwidth", `h', "Control Obs.", $Nl, "Treatment Obs.", $Nr, "Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "Yes")
}

//Field x Year FE
foreach h of numlist 1(2)7 {

	local covs award rank_norm inter
	if `h' == 1 {
		local covs award
	}

	reghdfe $Y `covs' $X $T if abs(rank_norm)<=`h', absorb(fieldtest_time) cluster(year_area) keepsingletons
	count if abs(rank_norm)<=`h' & award == 1
	global Nr = r(N)
	count if abs(rank_norm)<=`h' & award == 0
	global Nl = r(N)
	outreg2 using "Results/Online Appendix/Data/RDRank.txt",dec(3) dta bra append nonotes nocons drop($X $T) ///
	addtext("Bandwidth", `h', "Control Obs.", $Nl, "Treatment Obs.", $Nr, "Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "Yes")
}	
	
/*******************************************************************************
Appendix Table D.6: Robustness: Effect of National Distinction Award on Rarly-Career Earnings, Excluding Fields Rejected by Manipulation Test
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear

preserve
	use "Results/Main/Data/ManipulationTest.dta", clear
	keep exam_id exam_field Calonico_p_val*
	reshape long Calonico_p_val, i(exam_id exam_field) j(exam_year)
	count if Calonico_p_val < 0.05 
	scalar a = r(N)
	count if Calonico_p_val >= 0.05 & Calonico_p_val != .
	scalar b = r(N)
	di "The share of field-years rejected is " a/b
	tempfile pvals
	save `pvals', replace
restore
	

merge m:1 exam_year exam_id exam_field using `pvals'
drop _merge

gen manipulation_rejected = Calonico_p_val < 0.05 

//Area of Study x Year FE
rdrobust $Y $running if manipulation_rejected != 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S1)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
outreg2 using "Results/Online Appendix/Data/RDMain_NoManipulation.txt", dec(3) bra lab replace dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "No", "Covariates", "No") 

rdrobust $Y $running if manipulation_rejected != 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S2)
outreg2 using "Results/Online Appendix/Data/RDMain_NoManipulation.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "No") 

rdrobust $Y $running if manipulation_rejected != 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
outreg2 using "Results/Online Appendix/Data/RDMain_NoManipulation.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "Yes", "Field x Year FE", "No", "Test Scores", "Yes", "Covariates", "Yes") 

//Field x Year FE
rdrobust $Y $running if manipulation_rejected != 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE)
outreg2 using "Results/Online Appendix/Data/RDMain_NoManipulation.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "No", "Covariates", "No") 

rdrobust $Y $running if manipulation_rejected != 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE $T)
outreg2 using "Results/Online Appendix/Data/RDMain_NoManipulation.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "No") 

rdrobust $Y $running if manipulation_rejected != 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($FFE $T $X)
outreg2 using "Results/Online Appendix/Data/RDMain_NoManipulation.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr) ///
addtext("Area x Year FE", "No", "Field x Year FE", "Yes", "Test Scores", "Yes", "Covariates", "Yes") 



/*******************************************************************************
Appendix Table E.1: National Distinction Award and College Reputation
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009

//Full Sample
rdrobust $Y $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum $Y if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/CollegeReputationBw.txt", dec(3) bra dta lab replace nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)  ///
cti("Full Sample") addtext("Model", "RD") 

//Top 5 Colleges
rdrobust $Y $running if top5 == 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum $Y if award == 0 & abs($running) <= $MSE_Bw & top5 == 1
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/CollegeReputationBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)  ///
cti("Top 5") addtext("Model", "RD") 

//Top 6-20 Colleges
rdrobust $Y $running if top20 == 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum $Y if award == 0 & abs($running) <= $MSE_Bw & top20 == 1
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/CollegeReputationBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)  ///
cti("Top 6-20") addtext("Model", "RD")

//Low-ranked Colleges
rdrobust $Y $running if above20 == 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum $Y if award == 0 & abs($running) <= $MSE_Bw & above20 == 1
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/CollegeReputationBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)  ///
cti("Above 20") addtext("Model", "RD")
	
//Wage Gap:
//Middle-ranked Colleges
local sample_top20 ((top20 == 1 & $running >= 0) | (top5 == 1 & $running < 0))
rdrobust $Y $running if `sample_top20', $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum $Y if award == 0 & abs($running) <= $MSE_Bw & `sample_top20'
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/CollegeReputationBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)  ///
cti("Top 6-20") addtext("Model", "RD")

//Low-ranked Colleges
local sample_above20 ((above20 == 1 & $running >= 0) | (top5 == 1 & $running < 0))	
rdrobust $Y $running if `sample_above20', $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum $Y if award == 0 & abs($running) <= $MSE_Bw & `sample_above20'
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/CollegeReputationBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)  ///
cti("Above 20") addtext("Model", "RD") sortvar(reputation)


/*******************************************************************************
Appendix Table E.2: Effects on the Allocation of Skills
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y != .

rdrobust CIIUMatch2_fst $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum CIIUMatch2_fst if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/FieldIndustryBw.txt", dec(3) bra dta lab replace nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

rdrobust CIIUMatch2_fst $running if top5 == 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum CIIUMatch2_fst if award == 0 & abs($running) <= $MSE_Bw & top5 == 1
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/FieldIndustryBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

rdrobust CIIUMatch2_fst $running if top20 == 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)	
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum CIIUMatch2_fst if award == 0 & abs($running) <= $MSE_Bw & top20 == 1
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/FieldIndustryBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

rdrobust CIIUMatch2_fst $running if above20 == 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum CIIUMatch2_fst if award == 0 & abs($running) <= $MSE_Bw & above20 == 1
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/FieldIndustryBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

rdrobust $Y $running if transferable == 0, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum $Y if award == 0 & abs($running) <= $MSE_Bw & transferable == 0
scalar Mu = r(mean)
outreg2 using "Results/Online Appendix/Data/FieldIndustryBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

rdrobust $Y $running if transferable == 1, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum $Y if award == 0 & abs($running) <= $MSE_Bw & transferable == 1
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/FieldIndustryBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)


/*******************************************************************************
Appendix Table E.3: Effects on the Match Probability with High-Productivity Firms
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y != .

//Unconditional Ranking (within Industry Average Earnings)
rdrobust CIIURank_fst_sd $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum CIIURank_fst_sd if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/FirmProductivityBw.txt", dec(3) bra dta lab replace nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

//AKM Ranking
rdrobust AKMRank_fst_sd $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum AKMRank_fst_sd if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/FirmProductivityBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

//Unconditional Ranking (within Industry Average Earnings)
rdrobust CIIURank_avg_sd $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum CIIURank_avg_sd if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/FirmProductivityBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

//AKM Ranking
rdrobust AKMRank_avg_sd $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum AKMRank_avg_sd if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/FirmProductivityBw.txt", dec(3) bra dta lab append nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

/*******************************************************************************
Appendix Table E.4: Effects on Human Capital Accumulation
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y != .

rdrobust months_exam_to_grad $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum months_exam_to_grad if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/HumanCapitalBw.txt", dec(3) bra lab replace dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

rdrobust tot_classtaken $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum tot_classtaken if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/HumanCapitalBw.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

rdrobust classtaken_aftexam $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum classtaken_aftexam if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/HumanCapitalBw.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

rdrobust classpass_aftexam $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum classpass_aftexam if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/HumanCapitalBw.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

rdrobust gradsch $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(1) covs($S3)
scalar Nl = e(N_h_l)
scalar Nr = e(N_h_r)
sum gradsch if award == 0 & abs($running) <= $MSE_Bw
scalar Mu = r(mean) 
outreg2 using "Results/Online Appendix/Data/HumanCapitalBw.txt", dec(3) bra lab append dta nonotes ///
adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)


/*******************************************************************************
Appendix Table E.5: Effects on the Probability of Switching Jobs and Job Characteristics After Switching
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y!=.
foreach p in 1 2 {
	cap drop mover
	gen mover = 1 if (AKMRank_tau1 != AKMRank_tau2) & AKMRank_tau1 != . & AKMRank_tau2 != . 
	replace mover = 0 if mover == . & AKMRank_tau1 != . & AKMRank_tau2 != .
	rdrobust mover $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(`p') covs($S3)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum mover if award == 0 & abs($running) <= $MSE_Bw
	scalar Mu = r(mean)
	outreg2 using "Results/Online Appendix/Data/Job-LadderBW_P`p'.txt", dec(3) bra lab replace dta nonotes ///
	adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

	cap drop mover
	gen mover = 1 if (AKMRank_tau1 != AKMRank_tau2 & AKMRank_tau2 != AKMRank_tau3) & AKMRank_tau1 != . & AKMRank_tau2 != . & AKMRank_tau3 != . 
	replace mover = 0 if mover == . & AKMRank_tau1 != . & AKMRank_tau2 != . & AKMRank_tau3 != .
	rdrobust mover $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(`p') covs($S3)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum mover if award == 0 & abs($running) <= $MSE_Bw
	scalar Mu = r(mean)
	outreg2 using "Results/Online Appendix/Data/Job-LadderBW_P`p'.txt", dec(3) bra lab append dta nonotes ///
	adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

	rdrobust AKMRank_tau1_sd $running, $RDoptions h($MSE_Bw) b($MSE_Bias) p(`p') covs($S3)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum AKMRank_tau1_sd if award == 0 & abs($running) <= $MSE_Bw
	scalar Mu = r(mean)
	outreg2 using "Results/Online Appendix/Data/Job-LadderBW_P`p'.txt", dec(3) bra lab append dta nonotes ///
	adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

	local sample AKMRank_tau2 != . 
	rdrobust AKMRank_tau1_sd $running if `sample', $RDoptions h($MSE_Bw) b($MSE_Bias) p(`p') covs($S3)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum AKMRank_tau1_sd if award == 0 & abs($running) <= $MSE_Bw & `sample'
	scalar Mu = r(mean)
	outreg2 using "Results/Online Appendix/Data/Job-LadderBW_P`p'.txt", dec(3) bra lab append dta nonotes ///
	adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

	local sample AKMRank_tau3 != .
	rdrobust AKMRank_tau1_sd $running if `sample', $RDoptions h($MSE_Bw) b($MSE_Bias) p(`p') covs($S3)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum AKMRank_tau1_sd if award == 0 & abs($running) <= $MSE_Bw & `sample'
	scalar Mu = r(mean)
	outreg2 using "Results/Online Appendix/Data/Job-LadderBW_P`p'.txt", dec(3) bra lab append dta nonotes ///
	adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

	local sample AKMRank_tau2 != . 
	rdrobust AKMRank_delta12 $running if `sample', $RDoptions h($MSE_Bw) b($MSE_Bias) p(`p') covs($S3)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum AKMRank_delta12 if award == 0 & abs($running) <= $MSE_Bw & `sample'
	scalar Mu = r(mean)
	outreg2 using "Results/Online Appendix/Data/Job-LadderBW_P`p'.txt", dec(3) bra lab append dta nonotes ///
	adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

	local sample AKMRank_tau3 != . 
	rdrobust AKMRank_delta13 $running if `sample', $RDoptions h($MSE_Bw) b($MSE_Bias) p(`p') covs($S3)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum AKMRank_delta13 if award == 0 & abs($running) <= $MSE_Bw & `sample'
	scalar Mu = r(mean)
	outreg2 using "Results/Online Appendix/Data/Job-LadderBW_P`p'.txt", dec(3) bra lab append dta nonotes ///
	adds("Bandwidth", $MSE_Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)	
}

/*******************************************************************************
Appendix Table G.1: Effects on Allocation of Skills Using Different Measures
********************************************************************************/
use "Data/Finals/Estimation Sample.dta", clear
keep if exam_year <= 2009 & $Y != .
local i = 1
foreach y in fst ever {

	//Robustness Field-Industry Match
	cap drop CIIUMatch_overlap
	gen CIIUMatch_overlap = CIIUMatch2_`y'*CIIUMatch1_`y'
	cap drop CIIUMatch_union
	gen CIIUMatch_union = (CIIUMatch1_`y' + CIIUMatch2_`y' > 0) if CIIUMatch1_`y' != . & CIIUMatch2_`y' != .	
	
	//Measure2
	rdrobust CIIUMatch2_`y' $running , $RDoptions bwselect(mserd) p(1) covs($S3)
	global Bw = e(h_l)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum CIIUMatch2_`y' if award == 0 & abs($running) <= $Bw
	scalar Mu = r(mean)
	outreg2 using "Results/Online Appendix/Data/FieldIndustryRobust_`i'.txt", dec(3) bra lab replace dta nonotes ///
	adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)
		
	//Measure1
	rdrobust CIIUMatch1_`y' $running , $RDoptions bwselect(mserd) p(1) covs($S3)
	global Bw = e(h_l)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum CIIUMatch1_`y' if award == 0 & abs($running) <= $Bw
	scalar Mu = r(mean) 	
	outreg2 using "Results/Online Appendix/Data/FieldIndustryRobust_`i'.txt", dec(3) bra lab append dta nonotes ///
	adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

	//Union
	rdrobust CIIUMatch_union $running, $RDoptions bwselect(mserd) p(1) covs($S3)
	global Bw = e(h_l)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum CIIUMatch_union if award == 0 & abs($running) <= $Bw
	scalar Mu = r(mean) 	
	outreg2 using "Results/Online Appendix/Data/FieldIndustryRobust_`i'.txt", dec(3) bra lab append dta nonotes ///
	adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)

	//Overlap
	rdrobust CIIUMatch_overlap $running, $RDoptions bwselect(mserd) p(1) covs($S3)
	global Bw = e(h_l)
	scalar Nl = e(N_h_l)
	scalar Nr = e(N_h_r)
	sum CIIUMatch_overlap if award == 0 & abs($running) <= $Bw
	scalar Mu = r(mean)
	outreg2 using "Results/Online Appendix/Data/FieldIndustryRobust_`i'.txt", dec(3) bra lab append dta nonotes ///
	adds("Bandwidth", $Bw, "Control Obs.", Nl, "Treatment Obs.", Nr, "Mean Control", Mu)
	
	if `i' == 1 {
		preserve
		use "Results/Online Appendix/Data/FieldIndustryRobust_`i'_dta", clear
		save "Results/Online Appendix/Data/FieldIndustryRobust2.dta", replace
		restore	
	}
	if `i' > 1 {
		preserve
		use "Results/Online Appendix/Data/FieldIndustryRobust2.dta", clear
		append using "Results/Online Appendix/Data/FieldIndustryRobust_`i'_dta"
		save "Results/Online Appendix/Data/FieldIndustryRobust2.dta", replace
		restore
	}
	erase "Results/Online Appendix/Data/FieldIndustryRobust_`i'_dta.dta"
	erase "Results/Online Appendix/Data/FieldIndustryRobust_`i'.txt"
	
local i = `i' + 1
}

